package sn.hn.demo.unit;

import sn.hn.demo.MainActivity;
import sn.hn.demo.image.ImageData;
import sn.hn.demo.sprite.MySprite;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;

public class Boat extends Unit {
	public final int FLIP_VERTICAL = 1;
	public final int FLIP_HORIZONTAL = 2;
	public int mLength;
	public boolean isMove;
	public boolean isLeft;
	public Bitmap mBoat;
	public Bitmap mBoatLeft;
	public Bitmap mMoc;
	public MySprite mSprite;
	public MySprite mSpriteMoc;
	public MySprite mSpriteDay;
	public Boat(Context context, float x, float y, int length) {
		super(context, x, y);
		this.x = x;
		this.y = y;
		this.isLeft = false;
		this.isMove = false;
		this.mLength = length;
		getBitmap();
		mMoc = flip(mMoc, FLIP_HORIZONTAL);
		mSprite = new MySprite(x,y);
		mSprite.init(mBoat, mBoat.getWidth() , mBoat.getHeight(), 1, 0);
		mSpriteMoc = new MySprite(mSprite.xPos + mBoat.getWidth() - mMoc.getWidth()/2 - 2, mSprite.yPos + mBoat.getHeight()/2-3);
		mSpriteMoc.init(mMoc);
	}

	public void getBitmap() {
		mMoc = ImageData.mMoc;
		mBoat = ImageData.mBoat;
		mBoatLeft = flip(mBoat, FLIP_HORIZONTAL);
	}

	public void moveBoat() {
		if (this.x + mBoat.getWidth() >= MainActivity.SCREENWIDTH)
			this.x = MainActivity.SCREENWIDTH - mBoat.getWidth();
		if (this.x <= 0)
			this.x = 0;
		mSprite.xPos = this.x;
	}
	
	public Bitmap flip(Bitmap src, int type) {
		// create new matrix for transformation
		Matrix matrix = new Matrix();
		// if vertical
		if(type == FLIP_VERTICAL) {
			// y = y * -1
			matrix.preScale(1.0f, -1.0f);
		}
		// if horizonal
		else if(type == FLIP_HORIZONTAL) {
			// x = x * -1
			matrix.preScale(-1.0f, 1.0f);
		// unknown type
		} else {
			return src;
		}
		// return transformed image
		return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
	}
}
